package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.User;
import com.ruoyi.business.mapper.UserMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BindingMapper;
import com.ruoyi.business.domain.Binding;
import com.ruoyi.business.service.IBindingService;
import org.springframework.util.ObjectUtils;

/**
 * 学生绑定总Service业务层处理
 * 
 * @author lcz
 * @date 2025-03-31
 */
@Service
public class BindingServiceImpl implements IBindingService 
{
    @Autowired
    private BindingMapper bindingMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询学生绑定总
     * 
     * @param id 学生绑定总主键
     * @return 学生绑定总
     */
    @Override
    public Binding selectBindingById(Long id)
    {
        return bindingMapper.selectBindingById(id);
    }

    /**
     * 查询学生绑定总列表
     * 
     * @param binding 学生绑定总
     * @return 学生绑定总
     */
    @Override
    public List<Binding> selectBindingList(Binding binding)
    {
        return bindingMapper.selectBindingList(binding);
    }

    /**
     * 新增学生绑定总
     * 
     * @param binding 学生绑定总
     * @return 结果
     */
    @Override
    public int insertBinding(Binding binding)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long schoolId = sysUser.getSchoolId();
        binding.setSchoolId(schoolId);
        if (!ObjectUtils.isEmpty(binding.getUserId())) {
            User user = userMapper.selectUserById(binding.getUserId());
            if (!ObjectUtils.isEmpty(user)){
                binding.setAccount(user.getAccount());
                binding.setStudentName(user.getUserName());
            }
        }
        if (!ObjectUtils.isEmpty(binding.getAccount())) {
            User param = new User();
            param.setAccount(binding.getAccount());
            param.setSchoolId(schoolId);
            List<User> list = userMapper.selectUserList(param);
            if (list.size() > 0) {
                binding.setUserId(list.get(0).getId());
                binding.setStudentName(list.get(0).getUserName());
            }
        }
        binding.setCreateTime(DateUtils.getNowDate());
        return bindingMapper.insertBinding(binding);
    }

    /**
     * 修改学生绑定总
     * 
     * @param binding 学生绑定总
     * @return 结果
     */
    @Override
    public int updateBinding(Binding binding)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long schoolId = sysUser.getSchoolId();
        binding.setSchoolId(schoolId);
        if (!ObjectUtils.isEmpty(binding.getUserId())) {
            User user = userMapper.selectUserById(binding.getUserId());
            if (!ObjectUtils.isEmpty(user)){
                binding.setAccount(user.getAccount());
                binding.setStudentName(user.getUserName());
            }
        }
        if (!ObjectUtils.isEmpty(binding.getAccount())) {
            User param = new User();
            param.setAccount(binding.getAccount());
            param.setSchoolId(schoolId);
            List<User> list = userMapper.selectUserList(param);
            if (list.size() > 0) {
                binding.setUserId(list.get(0).getId());
                binding.setStudentName(list.get(0).getUserName());
            }
        }
        binding.setUpdateTime(DateUtils.getNowDate());
        return bindingMapper.updateBinding(binding);
    }

    /**
     * 批量删除学生绑定总
     * 
     * @param ids 需要删除的学生绑定总主键
     * @return 结果
     */
    @Override
    public int deleteBindingByIds(Long[] ids)
    {
        return bindingMapper.deleteBindingByIds(ids);
    }

    /**
     * 删除学生绑定总信息
     * 
     * @param id 学生绑定总主键
     * @return 结果
     */
    @Override
    public int deleteBindingById(Long id)
    {
        return bindingMapper.deleteBindingById(id);
    }

    @Override
    public void batchInsertBinding(List<Binding> bindingList) {
        bindingMapper.batchInsertBinding(bindingList);
    }
}
